public class 快乐数 {
    //https://leetcode.cn/problems/happy-number/
    //用快慢双指针的逻辑, 判断是否存在环
    //也可以使用hash
    public long powSum(long n) {
        //判断是否为快乐数, 加一下
        long cur = 0;
        while(n != 0){
            long t = n % 10;
            cur += (t) * (t);
            n /= 10;
        }
        return cur;
    }
    public boolean isHappy(long n) {
        long fast = n;
        long slow = n;
        do{
            slow = powSum(slow);
            fast = powSum(powSum(fast));
        }while(slow != fast);
        return slow != 1 ? false : true;
    }
}
